var SIGN_REGEXP = /([yMdhsm])(\1*)/g
var DEFAULT_PATTERN = 'yyyy-MM-dd'
function padding (s, len) {
  len = len - (s + '').length
  for (var i = 0; i < len; i++) {
    s = '0' + s
  }
  return s
}
function clone(obj) {
  var o
  if (typeof obj === 'object') {
    if (obj === null) {
      o = null
    } else {
      if (obj instanceof Array) {
        o = []
        for (var i = 0, len = obj.length; i < len; i++) {
          o.push(clone(obj[i]))
        }
      } else {
        o = {}
        for (var k in obj) {
          o[k] = clone(obj[k])
        }
      }
    }
  } else {
    o = obj
  }
  return o
}
export default {
  getQueryStringByName: function (name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = window.location.search.substr(1).match(reg)
    var context = ''
    if (r != null) {
      context = r[2]
    }
    reg = null
    r = null
    return context === null || context === '' || context === '' ? '' : context
  },
  formatDate: {
    format: function (date, pattern) {
      pattern = pattern || DEFAULT_PATTERN
      return pattern.replace(SIGN_REGEXP, function ($0) {
        switch ($0.charAt(0)) {
          case 'y':
            return padding(date.getFullYear(), $0.length)
          case 'M':
            return padding(date.getMonth() + 1, $0.length)
          case 'd':
            return padding(date.getDate(), $0.length)
          case 'w':
            return date.getDay() + 1
          case 'h':
            return padding(date.getHours(), $0.length)
          case 'm':
            return padding(date.getMinutes(), $0.length)
          case 's':
            return padding(date.getSeconds(), $0.length)
        }
      })
    },
    parse: function (dateString, pattern) {
      var matchs1 = pattern.match(SIGN_REGEXP)
      var matchs2 = dateString.match(/(\d)+/g)
      if (matchs1.length === matchs2.length) {
        var _date = new Date(1970, 0, 1)
        for (var i = 0; i < matchs1.length; i++) {
          var _int = parseInt(matchs2[i])
          var sign = matchs1[i]
          switch (sign.charAt(0)) {
            case 'y':
              _date.setFullYear(_int)
              break
            case 'M':
              _date.setMonth(_int - 1)
              break
            case 'd':
              _date.setDate(_int)
              break
            case 'h':
              _date.setHours(_int)
              break
            case 'm':
              _date.setMinutes(_int)
              break
            case 's':
              _date.setSeconds(_int)
              break
          }
        }
        return _date
      }
      return null
    }
  },
  cloneObj: function(obj) {
    return clone(obj)
  },
  // 继承属性
  extend: function (targetClass, sourceClass, bReserveOwnProperty) {
    for (var s in sourceClass) {
      if (!bReserveOwnProperty || targetClass.hasOwnProperty(s)) {
        sourceClass[s] = targetClass[s]
      }
    }
    return sourceClass
  },
  // 判断对象是否为空
  isEmptyObject: function (obj) {
    for (var key in obj) {
      return false
    }
    return true
  },
  isNumber: function (num, bPostive) {
    // bPostive 默认false
    return (bPostive ? (/^((\d+\.\d+)|(\d+))$/gi) : (/^-?((\d+\.\d+)|(\d+))$/gi)).test(num + '')
  },
  /**
   * 判断str是否以http://开头
   * @param {string} str
   * @return boolean
   */
  isHttp: function (str) {
    str += ''
    return !this.isEmpty(str) && (str.substr(0, 7).toLowerCase() === 'http://' || str.substr(0, 8).toLowerCase() === 'https://')
  },
  /**
   * 判断str是否email格式
   * @param {string} str
   * @return boolean
   */
  isEmail: function (str) {
    str += ''
    return (/^[\w]+@[\w]+(?:\.[a-zA-Z]+)+$/gi).test(str)
  },
  /**
   * 判断target是否为空
   * @param mixed target
   * @return boolean
   */
  isEmpty: function (obj) {
    // 本身为空直接返回true
    if (obj == null) {
      return true
    }
    // 然后可以根据长度判断，在低版本的ie浏览器中无法这样判断。
    if (obj.length > 0) {
      return false
    }
    if (obj.length === 0) {
      return true
    }
    if (typeof obj === 'object') {
      for (var key in obj) {
        return false
      }
      return true;
    }
  },
  /**
   * 判断obj是否为数组类型
   * @param mixed obj
   * @return boolean
   */
  isArray: function (obj) {
    return this.type(obj) === '[object Array]'
  },
  /**
   * 判断是否为整数或者正负整数
   * @param {number} num
   * @param {boolean} bPostive是否区分正负默认为false
   * @return {number}
   */
  isInt: function (num, bPostive) {
    // bPostive 默认false
    return (bPostive ? (/^\d+$/gi) : (/^-?\d+$/gi)).test(num + '')
  },
  /**
   * 判断是否为正浮点数或者负浮点数
   * @param {number} num
   * @param {boolean} bPostive是否区分正负默认为false
   * @return {number}
   */
  isFloat: function (num, bPostive) {
    // bPostive 默认false，默认判断负浮点数
    return (bPostive ? (/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/) : (/^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/)).test(num)
  },
  /**
   * 判断obj是否为function类型
   * @param mixed obj
   * @return boolean
   */
  isFunction: function (obj) {
    return this.type(obj) === '[object Function]'
  },

  /**
   * 判断obj是否定义
   * @param mixed obj
   * @return boolean
   */
  isUndefined: function (obj) {
    return obj === void 0
  },
  /**
   * 判断val是否为手机号
   * @param mixed val
   * @return boolean
   */
  isPhone: function (val) {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/
    if (!myreg.test(val)) {
      return false
    }
    return true
  },
  /**
   * 去除一纬数数组中重复数据 ['a','a','b','b']
   * @param mixed val
   * @return boolean
   */
  uniqueArr: function (arr) {
    let temp = [];
    for (let i = 0; i < arr.length; i++) {
      if (temp.indexOf(arr[i]) < 0) {
        temp.push(arr[i]);
      }
    }
    return temp;
  },
  /**
   * 得到一维非重数组元素位置 ['a','b','']
   * @param mixed val
   * @return boolean
   */
  itemPos: function (arr, item) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] === item){
        return i;
      }
    }
    return -1;
  },
  /**
   * 修剪字符串str两端空格
   * @param {string} str
   * @return {string}
   */
  trim: function (str) {
    return (str + '').replace(/(^\s*)|(\s*$)/gi, '');
  },
  /**
   * 为正则表达式字符串str转义
   * @param {string} str
   * @param {string} delimiter
   * @return {string}
   */
  regexpQuote: function(str, delimiter/*=''*/) {
    return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
  },
  /**
   * 扩展replace方法，在字符串str查找search，并替换所有
   * @param {string} str
   * @param {string} search
   * @param {string} replaceMent
   * @param {boolean} bIgnore是否区分大小写默认false
   * @return {string}
   */
  replaceEx: function(str, search, replaceMent, bIgnore/*=false*/) {
    return (str + '').replace(new RegExp(this.regexpQuote(search), bIgnore ? 'g' : 'gi'), replaceMent);
  },
  /**
   * 判断dom对象是否含有指定的className
   * @param string className
   * @return boolean
   */
  hasClass: function(dom, _className) {
    return (' ' + dom.className + ' ').indexOf(' ' + _className + ' ') > -1;
  },
  /**
   * 为dom对象追加className
   *
   * @param string className
   * @return HTMLElement
   */
  addClass: function(dom, _className) {
    if (!this.hasClass(dom, _className)) {
      dom.className = (dom.className ? (this.trim(dom.className) + ' ') : '') + _className;
    }
    return dom;
  },
  /**
   * 为dom对象移除className
   * @param string className
   * @return HTMLElement
   */
  removeClass: function(dom, className) {
    var c = dom.className;
    dom.className = this.trim(this.replaceEx(' ' + c + ' ', ' ' + className + ' ', ''));
    return dom;
  }
}
